package service

import (
	"io/fs"
	"path/filepath"
)

var (
	findList []string
	resultList []string
)

// 遍历文件的回调函数
func walkFunc(path string, info fs.FileInfo, err error) error {
	if info.IsDir() {
		return nil
	}
	for _, v := range findList {
		// 模糊匹配
		// if find := strings.Contains(info.Name(), v); find {
		// 	resultList = append(resultList, path)
		// 	return nil
		// }
		// 全匹配
		if info.Name() == v {
			resultList = append(resultList, path)
			return nil
		}
	}
	return nil
}

// FindByList 根据切片查询文件
func FindByList(root string, find []string) []string {
	findList = find
	err := filepath.Walk(root, walkFunc)
	if err != nil {
		panic(err)
	}
	return resultList
}
